// 创建store 仓库
import { legacy_createStore as createStore } from 'redux';
// ++++ 引入持久化两个方法,持久化仓库的方法 persistStore 和持久化reducer 方法persistReducer
import { persistStore, persistReducer } from 'redux-persist'

//+++++
import storage from 'redux-persist/lib/storage';

//在localStorge中生成key为root的值
const persistConfig = {
    key: 'root',
    storage,
    blacklist: []  //设置某个reducer数据不持久化，
}
// 定义默认state数据;
const defaultState = {
    userId: ''  //用户信息
}

// 创建reducer
const reducer = (state = defaultState, actions) => {
    if (actions.type == 'setuser') {
        return {
            ...state, // 保留原数据的操作
            userId: actions.payload
        }
    }

    return state
};

//+++
const myPersistReducer = persistReducer(persistConfig, reducer)

//+++做了修改
const store = createStore(myPersistReducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())

//+++
const persistor = persistStore(store)

export {
    store,
    persistor
} 
